import axios from 'axios'
import {utils} from './utils';
import store from '@/store'
import { Message } from 'element-ui'


export function ajaxPost(url,data,callback) {
    axios.post(url,data,{}).then((res)=>{
        callback(res.data);
    },(error)=>{
        console.log(error);
    });
}



export function addonAjaxPost(url,data,callback) {
    var _url = store.getters.userInfo.easyadmin_url + url;
    data.app_str = '';
    let AppStr = utils.cacheGet('addonappstr');
    if(AppStr) {
        data.app_str = AppStr;
    }

    data.realIp = store.getters.realIp;
    data.easyadmin_version = store.getters.userInfo.easyadmin_version;
    var _data = utils.returnData(data,store.getters.userInfo.easyadmin_appid,store.getters.userInfo.easyadmin_appsecret);
    axios.post(_url,_data,{}).then((res)=>{
        if (res.data.code !== 200) {
            if(res.data.code == 101) {
                utils.cacheSet('addonappstr',null);
            }

            Message({
                message: res.data.msg || '网络错误',
                type: 'error',
                duration: 5 * 1000
            })
            callback(res.data);
        }else{
            res.data.data = utils.aesDecrypt(res.data.data,res.data.sig);
            callback(res.data);
        }
        
    },(error)=>{
        console.log(error);
    });
}
